#include "TshEvent.h"
#include "InputEvent.h"
#include "InputEventCtrl.h"
#include "FolderInotify.h"
#include <stdio.h>
#include <pthread.h>

#define KEY 0x01
#define REL 0x02
#define ABS 0x03
InputEvent inputEvent;
FolderInotify folderInotify;


void inotifyEventLog(void *param, struct inotify_event *event)
{
    char path[100];
    if(event->cookie > 0)
    {
        printf("cokkie = %4d; ",event->cookie);
    }

    if(event->len)
    {
        if(event->mask & IN_CREATE)
        {
            printf("CREATE %s\n",event->name);
            sprintf(path,"/dev/input/%s", event->name);
            inputEvent.addAepollFile(path);
        }
        else if(event->mask & IN_DELETE)
        {
            printf("DELETE %s\n",event->name);
            sprintf(path,"/dev/input/%s", event->name);
            inputEvent.delAepollFile(path);
        }
    }
}


void* InotifyThreadFun(void * arg)
{
    folderInotify.inotifyEventDeal = inotifyEventLog;
    folderInotify.dealParam = arg;
    folderInotify.mask = IN_DELETE | IN_CREATE;

    if ( folderInotify.init() ) 
    {
        folderInotify.inotifyAddWatch((char*)"/dev/input/");
        folderInotify.run();
        return NULL;
    }
    else 
    {
        printf("init fail!\n");
    }
    return NULL;
}

void inputEventLog(void *param, int type, int code, int value) {
    static int x = 0;
    static int y = 0;
    static int id = 0;
    do {
        if (type == KEY) {
            printf("KEY--> type:%d, code:%d, value:%d \n", type, code, value);
            break;
        } 
        if (type == REL) {
            printf("REL--> type:%d, code:%d, value:%d \n", type, code, value);
            break;
        }
        if (type == EV_ABS) 
        {
            switch(code)
            {
                case ABS_MT_POSITION_X:   /* Center X touch position */
                    x = value;
                break;
                case ABS_MT_POSITION_Y:   /* Center Y touch position */
                    y = value;
                    printf("touch(x=%d,y=%d)\n", x,y);
                break;
                case ABS_MT_TRACKING_ID:  /* Unique ID of initiated contact */
                    id = value;
                    printf("touch_id=%d\n", id);
                break;
            }
        }
    } while(0);
    fflush(stdout);
}


void* pt_callback(void * arg)
{
    inputEvent.inputEventDeal = inputEventLog;
    inputEvent.dealParam = arg;

    if ( inputEvent.init() ) 
    {
        inputEvent.fileList.showList();
        inputEvent.run();
        return NULL;
    }
    else 
    {
        printf("init fail!\n");
    }
    return NULL;
}








int main()
{
    pthread_t ptid;
    pthread_t ftid;
    int ptre;
 

    ptre = pthread_create(&ptid, NULL, pt_callback, NULL);
    if (ptre != 0) 
    {
        printf("inputEvent thread create fail!\n");
    } 

    ptre = pthread_create(&ftid, NULL, InotifyThreadFun, NULL);
    if (ptre != 0) 
    {
        printf("inputEventCtrl thread create fail!\n");
    } 
    pthread_join(ptid, NULL);
    pthread_join(ftid, NULL);

    return 0;
}